//
//  UIColor+JwSkiner.m
//  JwPack
//
//  Created by 陈警卫 on 2024-08-06.
//

#import "UIColor+JwSkiner.h"
#import "JwSkiner.h"

@implementation UIColor (JwSkiner)

/// 根据colorKey获取颜色
+ (UIColor *)jw_colorWithSkinKey:(NSString *)key {
    SEL sel = NSSelectorFromString(key);
    id class = kJwSkiner.colorClass;
    
    if (![class respondsToSelector:sel]) {
        return [UIColor blackColor];
    }
    
    IMP imp = [class methodForSelector:sel];
    
    // typedef JwSkinerModel *(*MethodType)(id, SEL);
    // MethodType method = (MethodType)imp;
    
    JwSkinerModel *model = ((id(*)(id, SEL))imp)(class, sel);
    NSDictionary *colorParam = model.param;
    if (colorParam == nil) {
        return [UIColor blackColor];
    }
    UIColor *color = [self jw_colorWithSkinParam:colorParam];
    return color;
}

/// 根据param获取颜色
+ (UIColor *)jw_colorWithSkinParam:(NSDictionary *)param {
    NSString *currentSkinKey = kJwSkiner.currentSkinKey;
    NSString *value = param[currentSkinKey];
    if (!value) {
        if (kJwSkiner.skinKeys.count > 0) {
            NSString *key = kJwSkiner.skinKeys[0];
            value = param[key] ?: @"000000";
        }
    }
    UIColor *color = [UIColor jw_colorHexString:value];
    color = color ?: [UIColor blackColor];
    return color;
}

/// 根据params获取颜色
+ (UIColor *)jw_colorWithSkinParams:(NSArray *)params {
    if (params.count <= 0) {
        return [UIColor blackColor];
    }
    NSInteger currentSkinIndex = kJwSkiner.currentSkinIndex;
    currentSkinIndex = currentSkinIndex >= params.count ? 0 : currentSkinIndex;
    NSString *value = params[currentSkinIndex];
    if (!value) {
        value = params[0] ?: @"000000";
    }
    UIColor *color = [UIColor jw_colorHexString:value];
    color = color ?: [UIColor blackColor];
    return color;
}


@end
